const query = require('../db/query')
const { params } = require('../router')

// 删除
const doctorListDelete =(params)=>{
    let sql = `delete from doctorlist where doctorid = '${params.id}'`
    return query(sql)
}

// 新增
const doctorListAdd = (params) => {
    let sql = `INSERT INTO doctorlist (doctoriddoc,doctorname,doctordescword,doctoradministre,doctorcity,doctorimage,doctorstate,doctoradmin,doctorprice,doctorgood,doctorbrowse,doctortime,doctorflag) VALUES ('${params.id}','${params.name}','${params.descword}','${params.administre}','${params.city}','${params.image}','${params.state}','${params.admin}','${params.price}','${params.good}','${params.browse}','${params.time}',${params.flag})`
    return query(sql)
}

// 修改
const doctorListUpdate = (params) =>{
    let sql = `update doctorlist set doctorname = '${params.name}',doctordescword = '${params.descword}',doctoradministre = '${params.administre}',doctorcity = '${params.city}',doctorimage = '${params.image}',doctorstate = '${params.state}',doctoradmin = '${params.admin}',doctorprice = '${params.price}',doctorgood = '${params.good}',doctorbrowse = '${params.browse}',doctortime = '${params.time}',doctorflag = ${params.flag} where doctorid = '${params.id}'`
    return query(sql)
}

// 增加访问量
const doctorBrowse = (params) =>{
    let sql = `update doctorlist set doctorbrowse = doctorbrowse + 1 where doctorid = '${params.id}'`
    return query(sql)
}

// 服务过我的医生
const doctorServiceList = (params)=>{
    let sql = `select * from service a join doctorlist b join soco c on a.servicedoctorid = b.doctorid and b.doctoradmin = c.socoid`
    return query(sql)
}

// 新增服务过我的医生
const doctorServiceAdd = async (params)=>{
    let sql = await query(`select * from service where servicedoctorid='${params.doctorid}'`)
    let sql1 = ''
    if(sql.length > 0){
        sql1 = `DELETE FROM service where serviceid = ${sql.serviceid}`
    }else{
        sql1 = `INSERT INTO service (servicedoctorid) VALUES ('${params.doctorid}')`
    }
    return query(sql1)
}

module.exports = {
    doctorListDelete,
    doctorListAdd,
    doctorListUpdate,
    doctorServiceList,
    doctorServiceAdd,
    doctorBrowse
}